package com.wzh.ojbackendjudgeservice;


import com.wzh.ojModel.model.entity.JudgeContext;
import com.wzh.ojModel.model.entity.JudgeInfo;
import com.wzh.ojModel.model.entity.QuestionSubmit;
import com.wzh.ojbackendjudgeservice.strategy.JudgeStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wzh
 * 判题管理，简化调用
 */
@Service
public class JudgeManage implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private Map<String, JudgeStrategy> judges=new HashMap<>();


    public JudgeStrategy choose(String language){
        return judges.get(language);
    }


    @PostConstruct
    public void register(){
        Map<String, JudgeStrategy> judgeMap = applicationContext.getBeansOfType(JudgeStrategy.class);
        for (JudgeStrategy judgeStrategy : judgeMap.values()) {
            judges.put(judgeStrategy.getType(),judgeStrategy);
        }
    }

    public JudgeInfo doJudge(JudgeContext judgeContext){
        QuestionSubmit questionSubmit = judgeContext.getQuestionSubmit();
        String language = questionSubmit.getLanguage();
       return choose(language).doJudge(judgeContext);
/*        if ("java".equals(language)){
            return new JavaLanguageJudgeStrategy().doJudge(judgeContext);
        }else {
            return new DefaultJudgeStrategy().doJudge(judgeContext);
        }*/

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
           this.applicationContext=applicationContext;
    }
}
